//
//  BaseViewController.swift
//  Swift-Codedase
//
//  Created by 徐先生 on 2023/2/27.
//

import UIKit
import SnapKit
import Localize_Swift

class BaseViewController: UIViewController, UIGestureRecognizerDelegate {
    
    /// 状态栏样式
    var statusBarStyle = UIStatusBarStyle.default {
        didSet {
            // 主动触发来更新状态栏样式
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    /// 设置状态栏是否隐藏
    var statusBarHidden = false {
        didSet {
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    // 返回按钮
    lazy var backItem: UIBarButtonItem = {
        let item = UIBarButtonItem(
            image: "ic_back_333333".toImage,
            style: .done,
            target: self,
            action: #selector(backAction)
        )
        item.tintColor = UIColor(hex: "#333333")
        return item
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        // 它是一个类型为 UIExtendedEdge 的属性，指定边缘要延伸的方向，默认值是 UIRectEdge.all，即向四周边缘均延伸。
        edgesForExtendedLayout = .all
        // 设置导航栏的半透明效果，默认是true
        navigationController?.navigationBar.isTranslucent = true
        
        // 设置返回按钮
        navigationItem.leftBarButtonItem = backItem
        
        // 启用滑动返回（swipe back）
        navigationController?.interactivePopGestureRecognizer!.delegate = self
        
        //添加监听通知
        //        NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
        // https://www.jianshu.com/p/2535c985e27a
        // Swift网络权限允许点击之后的操作
        // NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
        
        // 监听语言的变化
        NotificationCenter.default.addObserver(self, selector: #selector(updateLanguageAction), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
        
        initNav()
    }
    
    // https://www.jianshu.com/p/2535c985e27a
    // Swift网络权限允许点击之后的操作
    //实现监听方法
    //    @objc func applicationDidBecomeActive(notification: NSNotification) {
    //        if(self.isViewLoaded && (self.view.window != nil)){
    //            debugPrint("*进入前台--------由另一个程序回到当前程序应用*")
    //            //这里是返回到当前App的方法
    //        }
    //    }
    
    // 1. 隐藏导航栏后，侧滑手势需要自己实现一个 UINavigationController 导航栏基类，不然侧滑手势不可用
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.children.count == 0 {
            return false
        }
        return true
    }
    
    // 设置状态栏样式
    // 重写该方法, 并设置 statusBarStyle
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return statusBarStyle
    }
    
    // 设置状态栏是否隐藏, true: 隐藏
    // 重写该方法, 并设置 statusBarHidden
    override var prefersStatusBarHidden: Bool {
        return statusBarHidden
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    fileprivate func initNav() {
        vhl_navSwitchStyle = .transition
        // vhl_navSwitchStyle = .fakeNavBar

    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
        xLog("✅ deinit \(self.className)")
    }
    
    override func willMove(toParent parent: UIViewController?) {
        super.willMove(toParent: parent)
        // xLog("willMove--\(String(describing: parent?.className))--\(self)")
        if parent == nil {
            // 侧滑过程中, 做些其他事情
        }
    }
    
    override func didMove(toParent parent: UIViewController?) {
        super.didMove(toParent: parent)
        // xLog("didMove--\(String(describing: parent?.className))--\(self)")
        if parent == nil {
            // 侧滑完成，做些其他事情
        }
    }
    
}

// MARK: - 响应事件
extension BaseViewController {
    
    /// 返回
    @objc func backAction() {
        back(true)
    }
    
    // 更新语言
    @objc func updateLanguageAction() {}
}
